/**
 * Copyright (c) 2005 - 2007 FeyaSoft Corp. All Rights Reserved.
 */
package com.feyaSoft.home.web.tutor;

import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

/**
 * @author F Zhuang
 * @Nov 7, 2007
 * 
 * This file is used to .....
 */
public class UserUtil {

	// log file
	private final static Logger log = Logger.getLogger(UserUtil.class);

	public static void jsonResponse(List allUsers, HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		UserJson tsJson = new UserJson();
		tsJson.setResults(allUsers);
		tsJson.setTotalCount(allUsers.size());
		log.debug("My result = " + tsJson.toJSONObject().toString());

		// check whether it is script Tag...
		// which is called by JSON
		boolean scriptTag = false;
		String cb = request.getParameter("callback");
		if (cb != null) {
			scriptTag = true;
			response.setContentType("text/javascript");
		} else {
			response.setContentType("application/x-json");
		}

		PrintWriter out = response.getWriter();
		if (scriptTag) {
			out.write(cb + "(");
		}
		response.getWriter().print(tsJson.toJSONObject());
		if (scriptTag) {
			out.write(");");
		}
	}
}
